var paperQuestion = {};

layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index'         //主入口模块
}).use(['index', 'table'], function () {
    var prefix = ctx + "/manage/paper/paperQuestion";
    var $ = layui.$, form = layui.form, table = layui.table;
    var paperId = $('#paperId').val() || '';
    var questionType = $('#questionType').val() || '';
    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });

    //事件
    var active = {
        batchdel: function () {
            // 遍历所有选择的行数据，取每条数据对应的ID
            var checkStatus = table.checkStatus('LAY-table')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要删除的数据！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });
            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/batchRemove", {"ids": ids}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        },
        add: function () {
            var data = {'btnHide': true, 'dialogArea': ['70%', '80%']};
            commonJs.openDialog('添加', prefix + '/add/' + paperId, data);
        }
    };

    $('.layui-btn.layuiadmin-btn-operate').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var cols = [{
        type: "checkbox",
        fixed: "left"
    }, {
        field: 'id',
        title: 'ID',
        width: 80
    }, {
        field: 'paperId',
        title: '试卷ID',
        width: 80
    }, {
        field: 'questionId',
        title: '问题ID',
        width: 80
    }, {
        field: 'subjectName',
        title: '科目',
        width: 220
    }, {
        field: 'sortNumber',
        title: '题号',
        edit: 'text',
        width: 80
    }, {
        field: 'type',
        title: '问题类型',
        width: 100,
        templet: function (d) {
            if (d.type == '1') {
                return '选择题'
            } else {
                return '综合题';
            }
        }
    }, {
        field: 'name',
        title: '问题'
    }, {
        field: 'answer',
        title: '答案',
        width: 80
    }, {
        title: "操作",
        width: 150,
        align: "center",
        fixed: "right",
        toolbar: "#table-btn-operate"
    }];

    if (questionType != '1') {
        delete cols[8];
    }

    //初始化layui的table
    commonJs.tableRender(table, cols, prefix + "/list", null, {'paperId': paperId});

    //初始化layui的table的tool操作
    table.on("tool(LAY-table)", function (e) {
        if ("del" === e.event) {
            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/remove", {"id": e.data.id}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        } else if ("view" === e.event) {
            var type = e.data.type;
            var questionId = e.data.questionId;
            commonJs.openDialog('查看', prefix + '/view/' + questionId + "?type=" + type, {'btnHide': true}, ['返回']);
        }
    });

    //table刷新
    paperQuestion.reload = function () {
        table.reload('LAY-table');
    }

    //监听单元格编辑
    table.on('edit(LAY-table)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段

        var params = {'id': data.id, 'clearField': 'clearField'};
        params[field] = value;
        if ('sortNumber' == field && value != '' && (!value || isNaN(value))) {
            commonJs.openMsg('cry', '只能填写数字！');
            return;
        }
        commonJs.openAjax(prefix + "/update", params, function (result) {
            if (result.code == 0) {
                commonJs.openMsg('success', '修改成功！', function () {
                    paperQuestion.reload();
                });
            } else {
                commonJs.openMsg('error', result.msg, function () {
                    paperQuestion.reload();
                });
            }
        });
    });
});